<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import type { FormRules, FormInstance } from 'element-plus'
import { Lock, User } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
const { push } = useRouter()
import { Login, GetValue } from '@/api/index'
import { objectToQueryParams } from '@/utils/objectToQuery'
import { useMessage } from '@/hooks/useMessage'
import CryptoJS from 'crypto-js'

const title = ref(import.meta.env.VITE_APP_TITLE)

const message = useMessage()

const form = ref({
  UserOrgID: '',
  UserID: '',
  UserPW: ''
})

interface RuleForm {
  UserOrgID: string
  UserID: string
  UserPW: string
}
const ruleFormRef = ref<FormInstance>()
const rules = reactive<FormRules<RuleForm>>({
  UserOrgID: [
    {
      required: true,
      message: '请选择组织',
      trigger: 'change'
    }
  ],
  UserID: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  UserPW: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

const pwRef = ref()
const focusPwRef = () => {
  pwRef.value.focus()
}

const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      // 使用MD5算法对密码进行加密
      const encrypted = CryptoJS.MD5(form.value.UserPW).toString()
      const encrypted2 = CryptoJS.MD5('AH-API-Showboard').toString()
      console.log(`output->`,encrypted2)
      const res = (await Login({ ...form.value, UserPW: encrypted })) as any
      if (res.code !== 200) {
        message.error(res.msg)
        return
      }
      //通过orgID查询orgName
      const result = orgList.value.filter((item: any)=>item.Value === form.value.UserOrgID) as any
      const userInfo = { orgName: result[0].Label, userID: form.value.UserID }
      localStorage.setItem('WMS_USER', JSON.stringify(userInfo))
      message.success(res.msg)
      push({ path: '/' })
    }
  })
}

const orgList = ref<any[]>([])
const getOrgList = async () => {
  const post = {
    groupName: 'Login',
    jobType: 'Login',
    operType: 'GetValue',
    key: 'FOrgID'
  }
  const inputStr = objectToQueryParams(post)
  const res = (await GetValue({ inputStr })) as any
  if (res.code !== 200) {
    message.error(res.msg)
    return
  }
  orgList.value = res.data.listInfo
}
onMounted(() => getOrgList())
</script>

<template>
  <div class="login-bg-box">
    <div class="mask">{{ title }}</div>
    <div class="login-main-box">
      <div class="login-img-box">
        <img src="../../assets/imgs/login-box.jpg" />
      </div>
      <div class="login-right-box">
        <h4>{{ title }}</h4>
        <el-form
          ref="ruleFormRef"
          :model="form"
          :rules="rules"
          label-width="auto"
          style="max-width: 600px; margin: 20px 0"
          @submit.prevent
        >
          <el-form-item label="组织" prop="UserOrgID">
            <el-select
              size="large"
              v-model="form.UserOrgID"
              filterable
              clearable
              style="width: 100%"
              placeholder="请选择组织"
              tabindex="1"
            >
              <el-option
                v-for="(item, index) in orgList"
                :key="index"
                :label="item.Label"
                :value="item.Value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="账号" prop="UserID">
            <el-input
              size="large"
              :prefix-icon="User"
              placeholder="请输入账号"
              v-model="form.UserID"
              tabindex="2"
              @keyup.enter.prevent="focusPwRef"
            />
          </el-form-item>
          <el-form-item label="密码" prop="UserPW">
            <el-input
              ref="pwRef"
              size="large"
              type="password"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              v-model="form.UserPW"
              show-password
              tabindex="3"
              @keyup.enter.prevent="onSubmit(ruleFormRef)"
            />
          </el-form-item>
        </el-form>
        <el-button
          size="large"
          type="primary"
          style="width: 84%"
          round
          class="ml-12"
          @click="onSubmit(ruleFormRef)"
          >登 录</el-button
        >
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.login-bg-box {
  position: relative;
  height: 100vh;
  width: 100vw;
  padding-top: 150px;
  /* background: linear-gradient(to right, #243ccf, #1a509a); */
}
.login-bg-box .mask {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  background: url('../../assets/imgs/login-bg.jpeg') no-repeat center center;
  background-size: cover;
  filter: blur(2px);
  color: #fff;
  text-align: center;
  font-size: 28px;
  line-height: 48px;
  font-weight: 700;
  // opacity: 0.5;
}
.login-main-box {
  position: absolute;
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 45%;
  height: 447px;
  padding-right: 20px;
  left: 50%;
  margin-left: -22%;
  top: 50%;
  margin-top: -220px;
  background: rgb(255, 255, 254, 0.9);
  border-radius: 10px;
}
.login-img-box {
  width: 50%;
  margin: 0;
}
.login-right-box h4 {
  padding-left: 60px;
  background: url('/public/logo.jpg') no-repeat left center;
  background-size: contain;
  font-size: 24px;
  font-weight: bold;
  letter-spacing: 6px;
  color: #409eff;
  text-align: center;
}
:deep(.el-form-item) {
  align-items: center;
}
</style>
